﻿package com.teaphy.coroutinecrawler.top_suspend

import kotlinx.coroutines.*
import kotlin.coroutines.suspendCoroutine

fun main() = runBlocking {
    println("currentThreadName -- runBlocking: ${Thread.currentThread().name}")
    println("start -->")

    val job1 = launch(Dispatchers.IO) {
        val data = generateNum()
        println("data: $data")
    }

    launch {
        delay(3000)
        job1.cancel()
    }

    println("--> end")
}

suspend fun generateNum(): Int {
    return suspendCoroutine {
        // 阻塞当前线程
        println("currentThreadName -- suspendCoroutine: ${Thread.currentThread().name}")
        Thread.sleep(5000)
        it.resumeWith(Result.success(1))
    }
}